package users

import (
	"context"
	"fmt"
	"star/internal/dao"
	"star/internal/model"
)

func GetUserList(ctx context.Context, in *model.UserListQuery) (list []model.UserItem, total int, err error) {
	db := dao.Users.Ctx(ctx)
	if in.Page == 0 {
		in.Page = 1
	}
	if in.Size == 0 {
		in.Size = 10
	}
	if len(in.Username) > 0 {
		db = db.WhereLike("username", fmt.Sprintf("%%%s%%", in.Username))
	}
	db = db.Order("created_at desc,id desc").Page(in.Page, in.Size)
	userdata, totalInt, err := db.AllAndCount(true)
	if err != nil {
		return
	}
	list = []model.UserItem{}
	err = userdata.Structs(&list)
	total = totalInt
	return
}
